import 'package:flutter_common_utils/src/extends/string_extends.dart';
import 'package:flutter_common_utils/src/hex/byte_utils.dart';

extension CUMapIterableExtension<K, V> on Iterable<MapEntry<K, V>> {
  /// 将 Iterable 转换为 Map，类似 Kotlin 的 associate
  Map<K, V> toMap() {
    return Map.fromEntries(this);
  }
}

extension CUMapExtension<K, V> on Map<K, V> {
  //实现两个map相加，使用+运算符
  Map<K, V> operator +(Map<K, V> other) {
    final result = Map<K, V>.from(this);
    result.addAll(other);
    return result;
  }
}

extension CUMapNullExtension<K, V> on Map<K, V>? {
  bool get isNullOrEmpty => this == null || this!.isEmpty;

  //获取一个bool
  bool getBool(K key, {bool defaultValue = false}) {
    if (this == null || this![key] == null) {
      return defaultValue;
    }
    if (this![key] is bool) {
      return this![key] as bool;
    }
    final str = this![key].toString();
    if (str.equals("true", true)) {
      return true;
    }
    if (str.equals("false", true)) {
      return false;
    }
    return this![key].toString().toInt().toBoolean();
  }

  //获取一个bool
  bool? getBoolNull(K key) {
    if (this == null || this![key] == null) {
      return null;
    }
    if (this![key] is bool) {
      return this![key] as bool;
    }
    final str = this![key].toString();
    if (str.equals("true", true)) {
      return true;
    }
    if (str.equals("false", true)) {
      return false;
    }
    return this![key].toString().toInt().toBoolean();
  }

  //获取一个int
  int getInt(K key, {int defaultValue = 0}) {
    if (this == null || this![key] == null) {
      return defaultValue;
    }
    if (this![key] is int) {
      return this![key] as int;
    }
    return this![key].toString().toIntOrNull() ?? defaultValue;
  }

  //获取一个int
  int? getIntOrNull(K key) {
    if (this == null || this![key] == null) {
      return null;
    }
    if (this![key] is int) {
      return this![key] as int;
    }
    return this![key].toString().toIntOrNull() ?? null;
  }

  //获取一个double
  double getDouble(K key, {double defaultValue = 0.0}) {
    if (this == null || this![key] == null) {
      return defaultValue;
    }
    if (this![key] is double) {
      return this![key] as double;
    }
    return this![key].toString().toDoubleOrNull() ?? defaultValue;
  }

  double? getDoubleOrNull(K key) {
    if (this == null || this![key] == null) {
      return null;
    }
    if (this![key] is double) {
      return this![key] as double;
    }
    return this![key].toString().toDoubleOrNull() ?? null;
  }

  // 获取一个String
  String getString(K key, {String defaultValue = ''}) {
    if (this == null || this![key] == null) {
      return defaultValue;
    }
    if (this![key] is String) {
      return this![key] as String;
    }
    return this![key].toString();
  }

  String? getStringOrNull(K key) {
    if (this == null || this![key] == null) {
      return null;
    }
    if (this![key] is String) {
      return this![key] as String;
    }
    return this![key].toString();
  }

  // 获取一个List
  List<V> getList(K key, {List<V> defaultValue = const []}) {
    if (this == null || this![key] == null) {
      return defaultValue;
    }
    if (this![key] is List) {
      return this![key] as List<V>;
    }
    return defaultValue;
  }

  List<V>? getListOrNull(K key) {
    if (this == null || this![key] == null) {
      return null;
    }
    if (this![key] is List) {
      return this![key] as List<V>;
    }
    return null;
  }
}

void main() {
  var map1 = {'a': 1, 'b': 2};
  var map2 = {'b1': 3, 'c': 4};
  print(map1 + map2);
  var list1 = [1, 2, 3];
  var list2 = [4, 5, 6];
  print(list1 + list2);
}
